package view.menu;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;
import model.*;
import view.PicturePanel;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;

/**
 * 
 * @author Sonny Sandberg
 */
public class Menu
{
    private JPanel 
            menuPanel;
    private ArrayList<view.Menu> menuStructure;

    public Menu()
    {
        menuStructure = new ArrayList();
        initMenuStructure();

        menuPanel = new JPanel(null);
        menuPanel.setOpaque(false);
        menuPanel.setVisible(false);
    }

    private void initMenuStructure()
    {
        menuStructure.add(new view.Menu("ORDRE", "top", (byte) 1, "orderContainer"));
        menuStructure.add(new view.Menu("LAGER", "top", (byte) 1, "adminProduct"));
        menuStructure.add(new view.Menu("LAGERSTATUS", "top", (byte) 2, "storageListPanel"));
        menuStructure.add(new view.Menu("ADMIN", "top", (byte) 1, "adminCustomer"));
        menuStructure.add(new view.Menu("LOG UD", "top", (byte) 1, null));

        menuStructure.add(new view.Menu("Moms / Miljø", "left", (byte) 2, "adminProgramValues"));
        menuStructure.add(new view.Menu("Bruger", "left", (byte) 2, "adminUser"));
        menuStructure.add(new view.Menu("Kunde", "left", (byte) 1, "adminCustomer"));
        menuStructure.add(new view.Menu("Varegruppe", "left", (byte) 2, "adminProductFamily"));
        menuStructure.add(new view.Menu("Kirkegård", "left", (byte) 1, "adminGraveYard"));
        menuStructure.add(new view.Menu("Backup", "left", (byte) 2, "backUp"));
    }

    public ArrayList<view.Menu> getMenuStructureArray(String topLeft, int accessLevel)
    {
        ArrayList<view.Menu> toReturn = new ArrayList();
        for (view.Menu value : menuStructure)
        {
            if (value.getAccesLevel() <= accessLevel && value.getTopLeft().equals(topLeft))
            {
                toReturn.add(value);
            }
        }

        return toReturn;
    }

    public void createTopMenu()
    {
        User loggedInUser = BSSingleton.getSingletonObject().getLoggedInUser();
        ArrayList<view.Menu> menu = getMenuStructureArray("top", loggedInUser.getAccessLevel());
        
        menuPanel.setBounds(55, 145, 960, 30);        
        
        int i = 0;
        for (view.Menu value : menu)
        {
            MenuLabelTop label = new MenuLabelTop(value.getName(), value);
            
            if (value.getName().equals("LOG UD"))
            {
                label.setBounds(840, 0, 120, 30);
            }
            else
            {
                label.setBounds(0 + (i * 140), 0, 120, 30);
            }
            
            menuPanel.add(label);
            ViewSingleton.getSingletonObject().getMenuLabels().add(label);
            i++;
        }
    }

    public void createLeftMenu()
    {
        User loggedInUser = BSSingleton.getSingletonObject().getLoggedInUser();        
        ArrayList<view.Menu> names = getMenuStructureArray("left", loggedInUser.getAccessLevel());

        menuPanel.setBounds(55, 245, 280, 500);

        JPanel leftMenuTop = new PicturePanel(280, 18, "/resources/images/sidebar_top.png").getBackgroundPanel();
        leftMenuTop.setLayout(null);
        leftMenuTop.setOpaque(false);
        leftMenuTop.setBounds(0, 0, 280, 18);
        
        JPanel leftMenuContent = new PicturePanel(280, 482, "/resources/images/sidebar_bg.png").getBackgroundPanel();
        leftMenuContent.setLayout(null);
        leftMenuContent.setOpaque(true);
        leftMenuContent.setBounds(0, 18, 280, 482);

        menuPanel.add(leftMenuTop);
        menuPanel.add(leftMenuContent);
        
        JLabel menuTitle = new JLabel("ADMIN");
        menuTitle.setBounds(20, 0, 240, 40);
        menuTitle.setFont(DesignLayout.PAGE_TITLE.deriveFont(15f));
        leftMenuContent.add(menuTitle);

        for (int i = 0; i < names.size(); i++)
        {
            MenuLabelLeft label = new MenuLabelLeft(names.get(i).getName(), names.get(i));
            label.setBounds(20, 40 + (i * 35), 240, 35);
            leftMenuContent.add(label);
        }
    }
    
    public void resetTextColor(Color color)
    {
        JPanel panel = (JPanel) menuPanel.getComponent(1);
        for (int i = 0; i < panel.getComponentCount(); i++)
        {
            JLabel label = (JLabel) panel.getComponent(i);
            label.setForeground(color);
        }
    }
    
    public void resetBackgroundColor(Color color)
    {
        for (int i = 0; i < menuPanel.getComponentCount(); i++)
        {
            JLabel label = (JLabel) menuPanel.getComponent(i);
            label.setBackground(color);
            label.setOpaque(false);
        }
    }

    public JPanel getPanel()
    {
        return menuPanel;
    }
}